package com.tcwlkj.uuService.widget.chatview;

import android.content.Context;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.tcwlkj.uuService.R;


/**
 * Created by ${tanbo} on 2017/8/10 0010.
 */

public class ChatBottomTextInputView extends LinearLayout implements
        View.OnClickListener, TextWatcher,View.OnFocusChangeListener{

private String TAG = "ChatBottomTextInputView";

public static interface Callback {

    /**
     * 点击了more按钮
     */
    void onTextMoreClick();

    void onLeftButtonClick();

    /**
     * 点击了发送消息
     */
    void onSendMessage(CharSequence value, int type);

    /**
     * 文本输入框的焦点发生变化
     */
    void onTextInputFocusChange(View v, boolean hasFocus);



}

    /**
     * 语音模式切换按钮
     */
    private Button questionModeButton;

    /**
     * 显示更多功能按钮
     */
    private ImageButton moreButton;

    /**
     * 文本输入框
     */
    private EditText contentEditView;

    /**
     * 发送按钮
     */
    private Button sendMsgButton;

    private Callback callback;

    @SuppressWarnings("deprecation")
    private ClipboardManager clip;

    public boolean isHideMoreButton = false;

    Context context;

//    /**
//     * 表情按钮
//     */
//    private ImageButton expressionBtn;

    public ChatBottomTextInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.chat_content_bottom_text_input,
                this);

        moreButton = (ImageButton) findViewById(R.id.chat_text_more_btn);
        moreButton.setOnClickListener(this);
        questionModeButton=(Button)findViewById(R.id.chat_question_mode_btn);
        questionModeButton.setOnClickListener(this);

        contentEditView = (EditText) findViewById(R.id.chat_content_et);
        contentEditView.setFocusable(true);
        contentEditView.setFocusableInTouchMode(true);
        contentEditView.requestFocus();
        contentEditView.addTextChangedListener(this);
        contentEditView.setOnFocusChangeListener(this);


        sendMsgButton = (Button) findViewById(R.id.chat_send_msg_btn);
        sendMsgButton.setOnClickListener(this);

        clip = (ClipboardManager) getContext().getSystemService(
                Context.CLIPBOARD_SERVICE);
    }


    public int getEditTextStart() {
        int editTextStart = -1;
        if (null != contentEditView) {
            editTextStart = contentEditView.getSelectionStart();
        }

        return editTextStart;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chat_text_more_btn:
                if (callback != null) {
                    callback.onTextMoreClick();
                }
                break;

            case R.id.chat_send_msg_btn:
                if (callback != null) {
                    callback.onSendMessage(contentEditView.getText(),
                            0);
                }
                break;
            case R.id.chat_question_mode_btn:
                if (callback != null) {
                    callback.onLeftButtonClick();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @SuppressWarnings("deprecation")
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //TCLog.d(TAG, "on text changed:" + s);
        if (TextUtils.isEmpty(s)) {
            return;
        }
        String text = s.toString();
        // 需要增加判断，在删减的时候，或者第一次进入界面时不会弹出。仅在输入@时才弹出 删除过程不弹出@
        //		if ((before < count) && (text.endsWith("@") || text.endsWith("＠"))) {
        //			PALog.d("text", "end with @");
        //			callback.onReminder();
        //		}
        if (clip != null) {
            if (TextUtils.isEmpty(clip.getText())) {
                return;
            }
            String content = clip.getText().toString().trim();

            if (content.startsWith("[msg:type]")
                    && s.toString().equalsIgnoreCase(content)) {
                if (callback != null) {
                    callback.onSendMessage(content.replace("[msg:type]", "")
                                    .trim(),
                            0);
                }
                clearSendText();
            }
        }

    }

    @Override
    public void afterTextChanged(Editable etText) {
        // 没有内容，发送按钮不可点击
        String s = etText.toString().trim();
        if (s == null || s.length() == 0) {
//            Tools.setVisibilitySafe(sendMsgButton, View.GONE);
//            if (isHideMoreButton) {
//                Tools.setVisibilitySafe(moreButton, View.GONE);
//            } else {
//                Tools.setVisibilitySafe(moreButton, View.VISIBLE);
//            }

            sendMsgButton.setEnabled(false);
        } else {
//            Tools.setVisibilitySafe(moreButton, View.GONE);
//            Tools.setVisibilitySafe(sendMsgButton, View.VISIBLE);
            sendMsgButton.setEnabled(true);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (callback != null) {
            callback.onTextInputFocusChange(v, hasFocus);
        }
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void clearSendText() {
        contentEditView.getText().clear();
    }

    public String getInputContentText() {
        return contentEditView.getText().toString();
    }

    public void setInputContentText(String text) {
        contentEditView.setText(text);
        contentEditView.setSelection(contentEditView.length());
    }

    public EditText getInputEditText() {
        return contentEditView;
    }

    public void appendContentText(String value) {
        if (TextUtils.isEmpty(value)) {
            return;
        }
        EditText contentEditView = getInputEditText();
        contentEditView.getText().append(value);
        contentEditView.setSelection(contentEditView.getText().length());
    }

    /**
     * 强制显示输入键盘,
     *
     * @since 不建议使用
     */
    public void showSoftKeyBoardView() {

        EditText et = getInputEditText();

        if (null != et) {
            et.setFocusable(true);
            et.setFocusableInTouchMode(true);
            et.requestFocus();

            InputMethodManager inputManager = (InputMethodManager) et
                    .getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(et, 0);

        }
    }

    public void setMoreButtonVisible(boolean visible) {
        if (visible) {
            moreButton.setVisibility(VISIBLE);
        } else {
            moreButton.setVisibility(GONE);
        }
    }



}

